package com.gupaoedu.vip.netty.io.nio.tomcat.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.QueryStringDecoder;

import java.util.List;
import java.util.Map;

/**
 * @program:gupaoedu-vip-netty-io
 * @description:Netty版本的HttpRequest
 * @author:Gongchao
 * @date:2020-04-28
 **/
public class GPRequest {

    private ChannelHandlerContext channelHandlerContext;

    private HttpRequest httpRequest;

    public GPRequest(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest) {
        this.channelHandlerContext = channelHandlerContext;
        this.httpRequest = httpRequest;
    }

    public String getUrl() {
        return httpRequest.uri();
    }

    public String getMethod() {
        return httpRequest.method().name();
    }

    public Map<String, List<String>> getParameters() {
        QueryStringDecoder queryStringDecoder = new QueryStringDecoder(httpRequest.uri());
        return queryStringDecoder.parameters();
    }

    public String getParameter(String name) {
        Map<String,List<String>> params = getParameters();
        List<String> param = params.get(name);
        if(null == param) {
            return null;
        }else{
            return param.get(0);
        }
    }
}
